<template>
    <el-dialog
            :title="!dataForm.notificationId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="通知ID，主键" prop="notificationId">
                        <el-input v-model.number="dataForm.notificationId" placeholder="通知ID，主键"></el-input>
                </el-form-item>
                <el-form-item label="任务ID，关联任务表的任务ID" prop="taskId">
                        <el-input v-model.number="dataForm.taskId" placeholder="任务ID，关联任务表的任务ID"></el-input>
                </el-form-item>
                <el-form-item label="接收通知的用户ID，关联用户表的用户ID" prop="userId">
                        <el-input v-model.number="dataForm.userId" placeholder="接收通知的用户ID，关联用户表的用户ID"></el-input>
                </el-form-item>
                <el-form-item label="通知类型" prop="notificationType">
                        <el-input v-model="dataForm.notificationType" placeholder="通知类型"></el-input>
                </el-form-item>
                <el-form-item label="通知发送时间" prop="notificationTime">
                        <el-date-picker
                                v-model="dataForm.notificationTime"
                                type="datetime"
                                placeholder="通知发送时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="通知是否已读，0表示未读，1表示已读" prop="isRead">
                        <el-input v-model="dataForm.isRead" placeholder="通知是否已读，0表示未读，1表示已读"></el-input>
                </el-form-item>
                <el-form-item label="通知内容" prop="notificationContent">
                        <el-input v-model="dataForm.notificationContent" placeholder="通知内容"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            notificationId: null,
            taskId: null,
            userId: null,
            notificationType: null,
            notificationTime: null,
            isRead: null,
            notificationContent: null
          },
          dataRule: {
            notificationId: [
                        { required: true, message: '通知ID，主键不能为空', trigger: 'blur' }
            ],
            taskId: [
                        { required: true, message: '任务ID，关联任务表的任务ID不能为空', trigger: 'blur' }
            ],
            userId: [
                        { required: true, message: '接收通知的用户ID，关联用户表的用户ID不能为空', trigger: 'blur' }
            ],
            notificationType: [
                        { required: true, message: '通知类型不能为空', trigger: 'blur' }
            ],
            notificationTime: [
                        { required: true, message: '通知发送时间不能为空', trigger: 'blur' }
            ],
            isRead: [
                        { required: true, message: '通知是否已读，0表示未读，1表示已读不能为空', trigger: 'blur' }
            ],
            notificationContent: [
                        { required: true, message: '通知内容不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.notificationId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.notificationId = id || 0
            if (this.dataForm.notificationId) {
              this.$http({
                url: this.$http.adornUrl(`/app/task_notification/info/${this.dataForm.notificationId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.apptasknotification
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/task_notification/${!this.dataForm.notificationId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>